package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Button;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.JianzhengStatusItemAdapter;
import com.xgkj.diyiketang.data.DesireInfo;
import com.xgkj.diyiketang.data.WitnessListInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.JianzhengrenListTask;
import com.xgkj.diyiketang.task.YuanwangDetailTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;
import com.xgkj.diyiketang.weidget.MyCustomListView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

public class YuanwangDetailActivity extends BaseActivity implements View.OnClickListener {

    private Context mContext;
    private RelativeLayout titleLayout;
    private TextView back;
    private RoundImageView headImage;
    private TextView nickname;
    private TextView createTime;
    private TextView yuanwangStatus;
    private TextView yuanwangTitle;
    private TextView yuanwangDesc;
    private TextView jiesuoFlower;
    private TextView startTime;
    private TextView endTime;
    private TextView jianzhengNum;
    private TextView jianzhengren;
    private MyCustomListView jianzhengList;
    private Button jianzhengBtn;
    private String mId;
    private String isConfirm;
    private int yuanwangDetailTaskId = -1;
    private int jianzhengrenListTaskId = -1;
    private JianzhengStatusItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yuanwang_detail);
        mContext = this;
        initView();
        getYuanwangDetail();
    }


    private void getYuanwangDetail() {
        YuanwangDetailTask yuanwangDetailTask = new YuanwangDetailTask(mId,isConfirm);
        yuanwangDetailTask.addListener(mListener);
        yuanwangDetailTaskId = getEatShowApp().getTaskManager().addTask(yuanwangDetailTask);
    }

    private void getJianzhengrenList(){
        JianzhengrenListTask jianzhengrenListTask = new JianzhengrenListTask(mId);
        jianzhengrenListTask.addListener(mListener);
        jianzhengrenListTaskId = getEatShowApp().getTaskManager().addTask(jianzhengrenListTask);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == yuanwangDetailTaskId) {
                yuanwangDetailTaskId = -1;
                if (data != null) {
                    DesireInfo info = (DesireInfo) data;
                    updateView(info);
                    getJianzhengrenList();
                }

                if (isConfirm != null && isConfirm.equals("1")){
                    isConfirm = null;
                    getYuanwangDetail();
                }
            }else if (taskId == jianzhengrenListTaskId){
                if (data != null){
                    List<WitnessListInfo> listInfos = (List<WitnessListInfo>) data;
                    if (listInfos != null && listInfos.size()>0){
                        mAdapter.updateData(listInfos);
                    }
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == yuanwangDetailTaskId) {
                yuanwangDetailTaskId = -1;
            }
        }
    };

    private void initView() {
        Intent intent = getIntent();
        if (intent != null){
            mId = intent.getStringExtra("ID");
        }

        if (mId == null){
            finish();return;
        }
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        jianzhengBtn = (Button) findViewById(R.id.jianzheng_btn);
        jianzhengBtn.setOnClickListener(this);
        headImage = (RoundImageView) findViewById(R.id.head_image);
        nickname = (TextView) findViewById(R.id.nickname);
        createTime = (TextView) findViewById(R.id.create_time);
        yuanwangStatus = (TextView) findViewById(R.id.yuanwang_status);
        yuanwangTitle = (TextView) findViewById(R.id.yuanwang_title);
        yuanwangDesc = (TextView) findViewById(R.id.yuanwang_desc);
        jiesuoFlower = (TextView) findViewById(R.id.jiesuo_flower);
        startTime = (TextView) findViewById(R.id.start_time);
        endTime = (TextView) findViewById(R.id.end_time);
        jianzhengNum = (TextView) findViewById(R.id.jianzheng_num);
        jianzhengren = (TextView) findViewById(R.id.jianzhengren);
        jianzhengList = (MyCustomListView) findViewById(R.id.jianzheng_list);
        mAdapter = new JianzhengStatusItemAdapter(mContext);
        jianzhengList.setAdapter(mAdapter);

    }

    private void updateView(DesireInfo info){
        String head_url = info.getUserLogo();
        if (head_url != null && head_url.length()>0){
            ImageCache.getInstance(mContext).loadBitmapToView(headImage,head_url,new ViewUtil.Size(ViewUtil.dip2px(mContext,40),ViewUtil.dip2px(mContext,40)));
        }
        try {
            String nick = URLDecoder.decode(info.getNickName(), "utf-8");
            nickname.setText(nick);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            String title = URLDecoder.decode(info.getDesireTitle(), "utf-8");
            yuanwangTitle.setText(title);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            String desc = URLDecoder.decode(info.getDesireContent(), "utf-8");
            yuanwangDesc.setText(desc);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (info.isWit()){
            jianzhengBtn.setVisibility(View.VISIBLE);
        }else {
            jianzhengBtn.setVisibility(View.GONE);
        }

        int status = info.getStatus();
        String yuanwang_status = "";
        if (status == 0){
            yuanwang_status = "未完成";
        }else if (status ==1){
            yuanwang_status = "完成";
            jianzhengBtn.setVisibility(View.GONE);
        }else if (status ==2){
            yuanwang_status = "作废";
            jianzhengBtn.setVisibility(View.GONE);
        }
        yuanwangStatus.setText(yuanwang_status);
        jiesuoFlower.setText(getString(R.string.flower_duo,info.getFlowerCount()));
        startTime.setText(info.getTimeStart());
        endTime.setText(info.getTimeEnd());
        createTime.setText(info.getCreateTime());
        jianzhengNum.setText(getString(R.string.jianzheng_num,info.getCount()));
        jianzhengren.setText(info.getWitness());

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jianzheng_btn:
                isConfirm = "1";
                getYuanwangDetail();
                break;
        }
    }
}
